﻿using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using digit.Core;
using SMS.UserManager;
using SMS.ServiceManager;

public partial class Modules_ListUserActivity : WebControlBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!ClientState.IsAuthenticated)
        {
            Response.Redirect("~/Login.aspx?session=timeout");
        }

        this.lnkDelete.Attributes.Add("onclick", "return confirm('Bạn đã chắc chắn xóa?');");
        this.lnkDeleteBottom.Attributes.Add("onclick", "return confirm('Bạn đã chắc chắn xóa?');");
        
        if ((ClientState.MemberInfo.UserType != 0 && ClientState.MemberInfo.UserType != 1 && !Util.GetAppSettings("ViewLogMember").Contains(ClientState.MemberInfo.Username)))
            Response.Redirect("Default.aspx?module=Warning");

        if (IsPostBack) return;

        this.BindMember();
        this.BindData(long.Parse(ddlUser.SelectedValue));
    }

    protected void lnkDelete_Click(object sender, EventArgs e)
    {
        int iDeleted = 0;

        //Search all checked rows to delete
        foreach (GridViewRow r in this.grvUserActivity.Rows)
        {
            //If checkbox is not checked then quit
            HtmlInputCheckBox cbx = (HtmlInputCheckBox)r.Cells[0].Controls[1];
            if (!cbx.Checked) continue;

            //If checkbox is checked then delete
            if (ActivitiesManager.Delete(Util.ToLong(cbx.Value)))
                iDeleted++;
        }

        //Display result of delete process
        if (iDeleted == 0)
        {
            this.lblError.Text = "Không có bản ghi nào được xóa!";
            return;
        }

        this.lblError.Text = "Đã xóa " + iDeleted.ToString() + " bản ghi!";

        this.BindData(long.Parse(this.ddlUser.SelectedValue));
    }

    protected void grvUserActivity_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.grvUserActivity.PageIndex = e.NewPageIndex;
        this.BindData(long.Parse(this.ddlUser.SelectedValue));
    }

    private void BindMember()
    {
        this.ddlUser.DataSource = MembersManager.SelectAll();
        this.ddlUser.DataBind();
        this.ddlUser.Items.Insert(0, new ListItem("-- Chọn người dùng --", "0"));
    }

    private void BindData(long userID)
    {
        try
        {
            //string strFilter = "";
            DataTable dt = ActivitiesManager.ListBy("", userID);

            if (dt != null && dt.Rows.Count > 0)
            {
                //DataView dv = dt.DefaultView;

                //if (this.ddlUser.SelectedValue != "") strFilter += "UserID LIKE '%" + this.ddlUser.SelectedValue + "%'";
                //dv.RowFilter = strFilter;

                this.grvUserActivity.DataSource = dt;
                this.grvUserActivity.DataBind();
                this.lblError.Text = "Hiện tại có tổng số <b>" + dt.Rows.Count + "</b> bản ghi";
            }
            else
            {
                lblError.Text = "Hiện tại chưa có bản ghi nào trong hệ thống";
            }
        }
        catch { }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        this.BindData(long.Parse(this.ddlUser.SelectedValue));
    }
}
